import axios from '@/js_sdk/gangdiedao-uni-axios'
import Vue from 'vue'
/** 
 *	post网络请求
 *  @param {string} url  请求地址
 *  @param {object} data 请求参数
 *  @callback then()	 请求成功
 *  @callback catch()    请求失败
 */
const $post = (url, data) => {
	return new Promise((resolve, reject) => {
		if (data.deviceUuid === 'not') {
			const Interval = setInterval(() => {
				if (Vue.prototype.deviceUuid !== 'not') {
					data.deviceUuid = Vue.prototype.deviceUuid
					clearInterval(Interval)
					axios.post(url, data, {
						'withCredentials': true
					})
						.then(res => {
							resolve(res.data)
						})
						.catch(err => {
							reject(err)
						})
				}
			}, 300);
		} else {
			axios.post(url, data, {
				'withCredentials': true
			})
				.then(res => {
					resolve(res.data)
				})
				.catch(err => {
					reject(err)
				})
		}
	})
}
/**  
 * 	async错误处理
 *  @param {promise}    请求promise
 *  @returns [data,err] [成功,失败]
 */
const awaitWarp = (promise) => {
	return promise.then(data => [data, null])
		.catch(err => [null, err])
}
//根据不同的环境更改不同的baseUrl
// const baseUrl = "https://android-back-test.huibenxia.com/"
const baseUrl = "https://android-back.huibenxia.com/"
const api = {
	//获取配置
	"getProfile": "mp/profile/get-profile.do",
	//语音合成
	"parseTextAudio": "nls/parse-text-to-audio.do",
	//热点数据
	"loadTopicData": "mp/topic/load-init-data.do",
	//课程数据
	"loadCourseData": "mp/course/load-init-data.do",
	//课程列表
	"loadCourseList": "/mp/course/list.do",
	//家长手册
	"loadNoteList": "mp/note/load-init-data.do",
	//评测数据
	"loadTrailData": "mp/trail/load-init-data.do",
	//问题数据
	"listQuestions": "mp/question/list.do",
	//更多数据
	"loadMoreData": "mp/search/load-init-data.do",
	//绘本数据
	"loadPicbookData": "mp/picbook/load-picbook.do",
	//所有绘本
	"loadPicbookInitData": "mp/picbook/load-init-data.do",
	//获取deviceUuid
	"getDeviceUuid": "mp/user/parse-code-to-device-uuid.do",
	//提交任务完成结果
	"addAnswer": "estimate/add-answer.do",
	//收藏绘本
	"collectPicbook": "mp/collect/collect-picbook.do",
	//课程分类
	"loadCourseListData": "mp/course/list-with-recommendable.do",
	//获取账户信息
	"loadAccount": "mp/account/load-account.do",
	//完善用户信息
	"fillUserInfo": "mp/user/fill-user-info.do",
	//搜索
	"search": "mp/search/search.do",
	//搜索绘本
	"searchPicbook": "mp/picbook/search.do",
	//搜索绘本
	"searchCourse": "mp/course/search-with-recommendable.do",
	//获取用户数据
	"loadUserData": "mp/user/load-user-data.do",
	//查询收藏列表
	"loadCollectList": "mp/collect/list.do",
	//查询足迹列表
	"loadTrackList": "mp/track/list.do",
	//跟踪足迹 绘本
	"trackPicbook": "mp/track/track-picbook.do",
	//跟踪足迹 测评
	"trackTrail": "mp/track/track-trail.do",
	//跟踪足迹 课程
	"trackCourse": "mp/track/track-course.do",
	//查看评测
	"loadScore": "mp/estimate/load-score.do",
	// 语音接口
	"audioText": "mp/question/is-audio-with-texts.do",
	//上传图片or 视频
	"upload": "mp/material/upload.do",
	//发布话题
	"addTopic": "mp/merchant/topic/add.do",
	//发布课程
	"addCourse": "mp/merchant/course/add.do",
	//发布手账
	"addNote": "mp/note/add.do",
	//不进行认证
	"applyCancel": "mp/merchant/apply/cancel.do",
	//开始商户认证
	"applyAdd": "mp/merchant/apply/add.do",
	//支付
	"prepay": "mp/wxpay/prepay-for-merchant.do",
	"afterPay": "mp/wxpay/update-after-pay.do",
	//查询评论
	"getComment": "mp/comment/list.do",
	//添加评论
	"addComment": "mp/comment/add.do",
	//查询点赞
	"getCollect": "mp/like/load-data.do",
	//点赞
	"saveCollect": "mp/like/save.do",
	//准备分享
	"shareReady": "mp/share-mission/ready.do",
	//已授权通知
	"authorizeNotify": "mp/share-mission/authorize-notify.do",
	//报名
	"register": 'mp/topic/register.do'

}
for (let key in api) {
	api[key] = baseUrl + api[key]
}

export default {
	$post,
	awaitWarp,
	baseUrl,
	api
}
